
#include "PersonManagement.h"
//2021/10/18 更换编译器为minGW后，错误消失

int PersonManagement::enterSize() {
    int size = -1;
    cout << "Please enter book size:";
    cin >> size;
    if(size != 0 && size != -1) return size;
    else{
        cout << "Automatically set to 100!" << endl;
        return 100;
    }
}

//开始工作
void PersonManagement::start() {
    //先初始化AddressBook对象
    auto *book = createBook(enterSize());
    //输出菜单
    showMenu();
    int status = 1;
    //用户输入不为0时便一直工作
    while (status != 0) {
        status = waitInput(book);
    }
}

//创建对象
AddressBook *PersonManagement::createBook(int bookSize) {
    return new AddressBook(bookSize);
}

//等待用户输入
int PersonManagement::waitInput(AddressBook *book) {
    cin.clear();
    cin.ignore();
    int mode = -1;
    cout << "Please enter a number:";
    cin >> mode;
    int r = functionSwitch(mode, book);
    return r;
}

/**
 * 通过用户输入的整数来选择工作模式并返回一个整数代表下一步的工作状态
 * @param mode 用户的输入
 * @param book
 * @return 返回用户输入的工作模式
 */
int PersonManagement::functionSwitch(int mode, AddressBook *book) {
    switch (mode) {
        default:
            showMenu();
            break;
        case 0:
            cout << "Exit." << endl;
            break;
        case 1:
            allFriend(book);
            break;
        case 2:
            addFriend(book);
            break;
        case 3:
            deleteFriend(book);
            break;
        case 4:
            break;
        case 5:
            break;
        case 6:
            break;
        case 7:
            getStorage(book);
            break;
    }
    return mode;
}

//输出菜单
void PersonManagement::showMenu() {
    cout << "*****FriendList******" << endl;
    cout << "***0.Exit************" << endl;
    cout << "***1.All friend******" << endl;
    cout << "***2.Add friend******" << endl;
    cout << "***3.Delete friend***" << endl;
    cout << "***4.Select friend***" << endl;
    cout << "***5.Update friend***" << endl;
    cout << "***6.Clear all*******" << endl;
    cout << "***7.Book storage****" << endl;
}

/**
 * 获取全部联系人
 * @param book
 */
void PersonManagement::allFriend(AddressBook *book) {
    //判断是否为空，为空则提示并直接返回
    if (book->isEmpty()) {
        cout << endl;
        cerr << "Empty list!" << endl;
        return;
    }
    string temp;
    stringstream ss;
    for (int i = 0; i < book->getLength(); i++) {
        temp = "";
        ss.clear();
        //将序号留存为string是为了方便给第二行添加等长度的缩进
        ss << (i + 1);
        ss << ".";
        ss >> temp;
        cout << temp;
        cout << "name:" << book->getPersonList()[i]->getName() << endl;
        //将temp的所有字符替换为空格并加在第二行前作为缩进
        for (int i = 0; i < temp.length(); i++) temp[i] = ' ';
        cout << temp << "phone:" << book->getPersonList()[i]->getPhone() << endl;
    }
}

/**
 * 添加联系人
 * @param book
 */
void PersonManagement::addFriend(AddressBook *book) {
    //判断是否已满，如果已满则提示并直接return
    if (book->isFull()) {
        cout << endl;
        cerr << "List is full!" << endl;
        return;
    }
    //标准输入数据
    string name;
    long phone;
    cout << "Please enter name:" << endl;
    cin >> name;
    cout << "Please enter phone:" << endl;
    cin >> phone;

    //将数据存储
    book->addPerson(new Person(name, phone));
    cout << "Success." << endl;
}

/**
 * 显示存储空间详情
 * @param book
 */
void PersonManagement::getStorage(AddressBook *book) {
    cout << "Book storage:" << book->getLength() << "/" << book->getMaxSize() << endl;
}

/**
 * 删除联系人
 * @param book
 */
void PersonManagement::deleteFriend(AddressBook *book) {
    if (book->isEmpty()) {
        cout << endl;
        cerr <<"Empty list!"<< endl;
        return;
    }
    int mode = -1;
    cout << "======delete mode======" << endl;
    cout << "===0.Cancel============" << endl;
    cout << "===1.Delete by name====" << endl;
    cout << "===2.Delete by phone===" << endl;
    cout << "===3.Delete by all===" << endl;
    cout << "=======================" << endl;
    reEnter:
    cout << "   Please enter delete mode:";
    cin >> mode;

    string name = "";
    long phone = -1;
    switch (mode) {
        case 0:
            cout << "Delete cancel." << endl;
            return;
        case 1:
            cin.clear();
            cout << "Please enter name:";
            cin >> name;
            break;
        case 2:
            cin.clear();
            cout << "Please enter phone:";
            cin >> phone;
            break;
        case 3:
            cin.clear();
            cout << "Please enter name:";
            cin >> name;
            cin.clear();
            cout << "Please enter phone:";
            cin >> phone;
            break;
        default:
            cin.clear();
            cin.ignore();
            goto reEnter;
    }
    cout << book->deletePerson(name,phone)<<endl;
}